package com.bsy.network.adapter

import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.internal.ObjectConstructor
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import com.hjq.gson.factory.element.TypeAdapterRuntimeTypeWrapper
import java.io.IOException
import java.lang.reflect.Type

/**
 * Created by Sincerity on 2022/7/14.
 * Copyright
 * 描述：
 */
class CustomTypeAdapter<E>(
    context: Gson?, elementType: Type?, elementTypeAdapter: TypeAdapter<E>?, private val constructor: ObjectConstructor<out MutableCollection<E>>
) : TypeAdapter<Collection<E>?>() {
    private val elementTypeAdapter: TypeAdapter<E> = TypeAdapterRuntimeTypeWrapper(context, elementTypeAdapter, elementType)

    @Throws(IOException::class)
    override fun read(reader: JsonReader): Collection<E>? {
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull()
            //                return null;
            return constructor.construct()
        }
        val collection = constructor.construct()
        reader.beginArray()
        while (reader.hasNext()) {
            val instance = elementTypeAdapter.read(reader)
            collection.add(instance)
        }
        reader.endArray()
        return collection
    }

    @Throws(IOException::class)
    override fun write(out: JsonWriter, collection: Collection<E>?) {
        if (collection == null) {
            out.beginArray()
            out.endArray()
            return
        }
        out.beginArray()
        for (element in collection) {
            elementTypeAdapter.write(out, element)
        }
        out.endArray()
    }
}
